// window.appApi.setAppSetting('countdown', 'time', 8*60*60)
let sourceCountdownSeconds =  10 * 60; // 默认10分钟
let countdownSeconds = sourceCountdownSeconds;
let countdownInterval = undefined;
window.appApi.getAppSetting('countdown').then(function (value) {
    if (value['time']) {
        sourceCountdownSeconds = countdownSeconds = value['time'];
    }
    displayCountdown(countdownSeconds);
})
// 监听重置事件
window.appApi.rendererListen('reset-countdown', function () {
    // 先清除定时器
    if (countdownInterval) {
        clearInterval(countdownInterval);
    }
    countdownSeconds = sourceCountdownSeconds;
    $('.app-countdown-btn>i').removeClass('fa-circle-pause').addClass('fa-circle-right').attr('data-status', 'start');
    displayCountdown(countdownSeconds);
});
// 监听重置事件
window.appApi.rendererListen('countdown-setting-change', function (event, settings) {
    // 先清除定时器
    if (countdownInterval) {
        clearInterval(countdownInterval);
    }
    if (settings['time'] && settings['time'] > 0) {
        countdownSeconds = sourceCountdownSeconds = settings['time'];
        $('.app-countdown-btn>i').removeClass('fa-circle-pause').addClass('fa-circle-right').attr('data-status', 'start');
        displayCountdown(countdownSeconds);
    }
});

function displayCountdown(second) {
    if (second < 0) {
        second = -second;
        $('.app').removeClass('app-normal').addClass('app-warning');
    } else {
        $('.app').removeClass('app-warning').addClass('app-normal');
    }
    const hours = Math.floor(second / 3600);
    const minutes = Math.floor((second - hours * 3600) / 60);
    const seconds = second - hours * 3600 - minutes * 60;
    const hoursStr = String(hours).padStart(2, '0');
    const minutesStr = String(minutes).padStart(2, '0');
    const secondsStr = String(seconds).padStart(2, '0');
    let html = '';
    html += `<span class="app-countdown-content-hours">${hoursStr}</span>`;
    html += '<span class="app-countdown-content-hours app-countdown-content-hours-colon">:</span>';
    html += `<span class="app-countdown-content-minutes">${minutesStr}</span>`;
    html += '<span class="app-countdown-content-minutes app-countdown-content-minutes-colon">:</span>';
    html += `<span class="app-countdown-content-seconds">${secondsStr}</span>`;
    $('.app-countdown-content').html(html);
}

function startCountdown() {
    countdownInterval = setInterval(function () {
        countdownSeconds--;
        displayCountdown(countdownSeconds);
    }, 1000);
}

function pauseCountdown() {
    if (countdownInterval) {
        clearInterval(countdownInterval);
    }
}

$(document).ready(function () {
    $('.app-countdown-btn>i').click(function () {
        const btnStatus = $(this).attr('data-status');
        if (btnStatus === 'start') {
            $(this).removeClass('fa-circle-right').addClass('fa-circle-pause');
            $(this).attr('data-status', 'pause');
            startCountdown();
        } else {
            $(this).removeClass('fa-circle-pause').addClass('fa-circle-right');
            $(this).attr('data-status', 'start');
            pauseCountdown();
        }
    });
});
